home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Add-Ons / MicroPhone / Open Mike™ / Open Mike™ 011 / Mike's Folder / Hacker's Hangout < prev    next >
Encoding:
Text File  |  1993-11-01  |  5.5 KB  |  94 lines  |  [TEXT/ttxt]

  1. Hacker's Hangout
  2. ================
  3. Copyright © 1993 by Celestin Company
  4. All rights reserved.
  5.  
  6. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without permission in writing from the publisher. However, you are permitted to make copies of this work, printed or otherwise, as long as said copies are for your personal use only.
  7.  
  8. Introduction
  9. ------------
  10. Do you have a file that you transferred from some Unix host, and you forgot to transfer using the text mode of your protocol? Did it come across with linefeed characters instead of carriage returns? If you open up the file with a text editor and see funny boxes at the beginning of each line, that's probably what happened. Have no fear. In this Hacker's Hangout, we show you a way of converting those linefeed characters to carriage returns.
  11.  
  12. To perform this hack, we take advantage of a standard MicroPhone XFCN called ReplaceStr. ReplaceStr will replace all occurrences of a certain string with another string. We feed 30K chunks of the text file to ReplaceStr, asking it to replace linefeed characters (^J) with carriage returns (^M). We do 30K chunks because MicroPhone variables and XFCNs can handle only 30K of text at a time. We also create a new file and do our business with this file. Only after we are done and verify that we were successful do we delete the original file and rename the new one.
  13.  
  14. Instructions - Put the module called 'TenToThirteen' in your Module Folder, which is in the same location as the MicroPhone application. If you do not have a Module Folder, create one. Open any MicroPhone settings document. Create the following script:
  15.  
  16.   Set Variable * TenToThirteen from TEXT File Dialog "'File to Convert?'"
  17.   If Expression "Length(TenToThirteen) > 0"
  18.     Do Script "'Main','TenToThirteen'"
  19.   End If
  20.  
  21. That's it.
  22.  
  23. Theory
  24. ------
  25. So, what's behind this hack? Let's take a quick look at the scripts in TenToThirteen. Here are the scripts in the TenToThirteen module:
  26.  
  27. Main
  28. TenToThirteen
  29. FileOpen
  30. FileClose
  31. FileReplace
  32. FileInfoGet
  33. FileInfoSet
  34. ChunkRead
  35. ChunkWrite
  36.  
  37. Main - this script simply calls the script TenToThirteen. It is used to provide a single entry point into the module. This way, you don't have to know the name of a particular script. Just call the main script in any of the modules that have been provided with Open Mike and they will do the right thing.
  38.  
  39. TenToThirteen - This script does the work of converting the file, by calling several other scripts. First, it calls FileInfoGet to get the type and creator of the source file. Then, it calls FileOpen to open the source and destination files. Next, it repeatedly calls ChunkRead and ChunkWrite until the entire source file has been processed. Then, it calls FileClose. Finally, it calls FileReplace, to replace the source file with the new file it has created:
  40.  
  41. If Expression "Exists(TenToThirteen)"
  42.   Do Script * "'FileInfoGet'"
  43.   If Success
  44.     Do Script * "'FileOpen'"
  45.     If Success
  46.       While Expression "not EOF(TenToThirteen)"
  47.         Do Script * "'ChunkRead'"
  48.         Set Variable * myChunk from Expression "ReplaceStr('^J','^M',myChunk)"
  49.         Do Script * "'ChunkWrite'"
  50.       End While
  51.       Do Script * "'FileClose'"
  52.       Do Script * "'FileReplace'"
  53.       If Success
  54.         Return Success
  55.       Else
  56.         Remark "--- could not replace old source file"
  57.         Return Failure
  58.       End If
  59.     Else
  60.       Remark "--- could not open files"
  61.       Do Script * "'FileClose'"
  62.       Return Failure
  63.     End If
  64.   Else
  65.     Remark "--- could not get file information"
  66.     Return Failure
  67.   End If
  68. Else
  69.   Remark "--- variable TenToThirteen does not exist"
  70.   Return Failure
  71. End If
  72.  
  73. FileOpen - this script opens the source and destination files. It gives the destination the same name as the source, with '.b' appended to the name. If the name is greater than 31 characters, it concatenates the name until it equals 31 characters. If any problems are encountered, it returns failure. Otherwise, it returns success.
  74.  
  75. FileClose - this script closes the source and destination files.
  76.  
  77. FileReplace - this script replaces the old source file with the newly created file. If any problems are encountered, it returns failure. Otherwise, it returns success.
  78.  
  79. FileInfoGet - this script gets the file information for the source file using the FileTYPE and FileCRTR functions. These values are stored in the variables myFileTYPE and myFileCRTR.
  80.  
  81. FileInfoSet - this script sets the file information for the new file using the stored TYPE and CREATOR information in the myFileTYPE and myFileCRTR variables.
  82.  
  83. ChunkRead - this script reads up to 30K of text from the source file and stores it in the variable called myChunk.
  84.  
  85. ChunkWrite - this script takes the value of myChunk and writes it to the destination file.
  86.  
  87. Where To Go From Here
  88. ---------------------
  89. TenToThirteen is an ideal candidate for an XCMD, because all we are dealing with are a source and destination file. MicroPhone scripts are powerful and flexible, but when you need to process a huge file, maybe a 20 megabyte session file from your frenzied weekend, you might find MicroPhone a tad slow. Maybe we'll convert TenToThirteen to an XCMD in a future issue of Open Mike.
  90.  
  91. Important Note
  92. --------------
  93. Hacks are not guaranteed to work, although there is no reason why they shouldn't work. Every care has been taken to make sure that they work on the hacker's machine, but they may do other things on your machine. Of course, if you encounter problems, you may feel free to tweak the hack to run on your machine.
  94.